A frequently asked question is, “What’s the simplest way to begin using AWS?” While there are several well-established methods, including AWS Elastic Beanstalk, serverless computing is emerging as a rapidly growing alternative.
Serverless computing enables you to develop and operate applications and services without the need to manage servers. At the heart of serverless computing on AWS is the AWS Lambda service. Additionally, AWS offers several other services that enhance serverless architectures, such as Amazon API Gateway for creating RESTful APIs alongside Lambda and Amazon DynamoDB, a NoSQL cloud database service that alleviates the need for setting up a database cluster.
The following diagram illustrates a fully serverless architecture.
The lower section of the diagram showcases a RESTful API service. API Gateway handles API requests and responses, mapping them to the Lambda functions that encompass business logic. DynamoDB serves as the persistence layer. The upper section of the diagram represents the frontend. Amazon S3 hosts static website assets, such as an AngularJS or React app, functioning as a fully managed service that removes the necessity of operating a fleet of frontend servers. Amazon CloudFront, a content delivery network (CDN), is positioned in front of S3 to ensure efficient delivery of website content from edge locations globally.
Previously, a challenge with deploying serverless applications was achieving efficiency. However, AWS now offers a native solution: the AWS Serverless Application Model (AWS SAM). With AWS SAM, you can effortlessly manage your serverless deployment utilizing a straightforward YAML-based description language and just two AWS CLI commands. Transitioning to a serverless model might now be the most accessible way to begin your journey with AWS, particularly for those less familiar with infrastructure management.
In this article, I’m excited to introduce the inaugural component of a broader initiative: the AWS Startup Kit. This kit delivers actionable guidance on how to embark on your AWS journey, featuring several example workloads that utilize technologies frequently adopted by startups. A “workload” refers to one or more interconnected applications operating on AWS that provide business or operational value, such as a RESTful API for your customers or a batch processing job for analytics.
The component highlighted here is the Startup Kit Serverless Workload: a sample RESTful API for a TODO app developed using the Lambda Node.js 4.3 runtime and deployed through SAM. You can explore the code on GitHub in the startup-kit-serverless-workload repository. If you haven’t done so yet, you’ll need to establish an AWS account and set up the AWS CLI (more details can be found here).
Advantages of Serverless Architecture
Adopting a serverless architecture allows you to harness many benefits outlined in the AWS Well-Architected framework. While an exhaustive review of the Well-Architected framework is beyond this article’s scope, it’s beneficial to quickly assess how the five pillars apply to this architecture.
In the following summary chart, “HA” stands for high availability, “OS” refers to operating system, and “IAM” denotes the AWS Identity and Access Management service, which empowers you to securely control access to AWS services and resources.
Component | Security | Reliability | Performance Efficiency | Cost Optimization | Operational Excellence |
---|---|---|---|---|---|
API Gateway | HTTPS by default; throttling can be configured; secure calls with IAM or bearer token auth. | AWS manages HA and scaling; calls aren’t throttled unless specified. | Result caching can be enabled; serverless resources consumed only as needed. | No need to manage a fleet of reverse proxies for API calls. | Automate with SAM; easy API versioning and deployment management. |
Lambda | AWS managed OS; a Lambda function’s access to AWS resources is limited by IAM roles. | AWS manages HA and scaling; asynchronous invocations are retried and may go to a dead letter queue (DLQ). | Serverless resources consumed only as needed. | Cost correlates with uptime; no charges if idle. | Automate with SAM; basic metrics available; logs accessible via CloudWatch. |
DynamoDB | IAM allows fine-grained access controls; calls tracked with AWS CloudTrail. | AWS manages HA and scaling; data is replicated thrice in an AWS Region. | Serverless resources consumed only as needed. | No cluster management needed; no hardware capacity guessing. | Automate with SAM; basic metrics shown in DynamoDB console; use DynamoDB Streams for tracking changes. |
This analysis merely scratches the surface of the Well-Architected framework. As you proceed with constructing your serverless architecture on AWS, be sure to refer to the Well-Architected framework’s whitepaper for additional insights and ways to enhance your architecture.
Deploying the Workload with SAM
SAM provides a model for defining serverless applications. To leverage SAM, you describe your serverless resources using YAML (or JSON) syntax in a template file, then package and deploy your code using a pair of AWS CLI commands. SAM is an open-source project available on GitHub.
SAM supports three primary types of serverless resources: “Function” (via Lambda), “Api” (via API Gateway), and “SimpleTable” (via DynamoDB). It also allows the specification of event sources for Functions (like an Api) and properties such as environment variables for Functions. To simplify your SAM template, you can specify an Api as an event source for a Function, eliminating the need to declare an Api resource explicitly, as SAM will handle it for you.
The SAM template below demonstrates how easy it is to define a Function resource. The first Function, CreateFunction, implements an API call to create a new TODO item in the TODO app’s DynamoDB table. The relationship of CreateFunction to the rest of the serverless application is clearly outlined in the SAM template. For instance, to detail how CreateFunction interacts with the DynamoDB table, the template assigns an IAM policy that includes DynamoDB write permissions and specifies an environment variable referencing the DynamoDB table. An Api event is defined for invoking CreateFunction, described by the path and HTTP method—specifically POST. The other Functions in the SAM template follow the same foundational pattern.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: RESTful API for a TODO app, backed by a SimpleTable (DynamoDB) resource.
Resources:
CreateFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.create
Runtime: nodejs4.3
Policies: AmazonDynamoDBFullAccess
Environment:
Variables:
TABLE_NAME: !Ref Table
Events:
PostResource:
For further insights into this topic, you can explore this excellent resource. If you’re looking for more engaging content, check out this blog post. Additionally, for authoritative information, visit Chanci Turner’s site.
Leave a Reply